package Controller
{
	import mx.collections.ArrayCollection;
	import mx.containers.HBox;
	import mx.core.FlexGlobals;
	import mx.resources.IResourceManager;
	import mx.resources.ResourceManager;
	
	import spark.components.TextInput;
	
	import Model.ComplexSolidComponent;
	import Model.HeatedSpaceArea;
	import Model.HeatedSpaceVolume;
	import Model.SolidComponent;
	import Model.TransparentComponent;
	
	import View.area.AreaInput;

	public class AreasController
	{
		
		public function AreasController()
		{
		}

//Heated Space Area Methods
	
		public function addHeatedSpaceArea(heatedSpaceArea:HeatedSpaceArea):void
		{
			FlexGlobals.topLevelApplication.project.areas.addHeatedSpaceArea(heatedSpaceArea);
		}
		
		public function removeHeatedSpaceArea(heatedSpaceArea:HeatedSpaceArea):void
		{
			FlexGlobals.topLevelApplication.project.areas.removeHeatedSpaceArea(heatedSpaceArea);
		}
		
		public function getHeatedSpaceAreaById(id:String):HeatedSpaceArea
		{
			return	FlexGlobals.topLevelApplication.project.areas.getHeatedSpaceAreaById(id);
		}
		
		public function removeHeatedSpaceAreaByID(id:String):void
		{
			this.removeHeatedSpaceArea(this.getHeatedSpaceAreaById(id));
		}
		
		public function getAllHeatedSpaceAreas():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.heatedSpaceAreas;
		}
		
		public function updateHeatedSpaceAreaUISection():void
		{
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).updateHeatedSpaceAreaUISection(FlexGlobals.topLevelApplication.project.areas.heatedSpaceAreas);
		}
		
		public function getHeatedSpaceAreas():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.heatedSpaceAreas;
		}

		public function getTotlaHeatedSpaceArea():Number
		{
			return FlexGlobals.topLevelApplication.project.areas.totalHeatedSpaceArea;
		}
//Heated Space Volume Methods
		
		public function addHeatedSpaceVolume(heatedSpaceVolume:HeatedSpaceVolume):void
		{
			FlexGlobals.topLevelApplication.project.areas.addHeatedSpaceVolume(heatedSpaceVolume);
		}
		
		public function removeHeatedSpaceVolume(heatedSpaceVolume:HeatedSpaceVolume):void
		{
			FlexGlobals.topLevelApplication.project.areas.removeHeatedSpaceVolume(heatedSpaceVolume);
		}
		
		public function removeHeatedSpaceVolumeById(id:String):void
		{
			this.removeHeatedSpaceVolume(this.getHeatedSpaceVolumeById(id));
		}
		
		public function getHeatedSpaceVolumeById(id:String):HeatedSpaceVolume
		{
			return FlexGlobals.topLevelApplication.project.areas.getHeatedSpaceVolumeById(id);
		}
		
		public function updateHeatedSpaceVolumeUISection():void
		{
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).updateHeatedSpaceVolumeUISection(FlexGlobals.topLevelApplication.project.areas.heatedSpaceVolumes);
		}
		
		public function getAllHeatedSpaceVolumes():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.heatedSpaceVolumes;
		}
		
		public function getHeatedSpaceVolumes():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.heatedSpaceVolumes;
		}

		public function getTotalHeatedSpaceVolume():Number
		{
			return FlexGlobals.topLevelApplication.project.areas.totalHeatedSpaceVolume;
		}
//Solid Components Methods
		
		public function getSolidComponents():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.solidComponents;
		}
		
		public function addSolidComponent(solidComponent:SolidComponent):void
		{
			FlexGlobals.topLevelApplication.project.areas.addSolidComponent(solidComponent);		
		}
		
		public function removeSolidComponent(solidComponent:SolidComponent):void
		{
			FlexGlobals.topLevelApplication.project.areas.removeSolidComponent(solidComponent);
		}
		
		public function removeSolidComponentById(id:String):void
		{
			FlexGlobals.topLevelApplication.project.areas.removeSolidComponent(this.getSolidComponentById(id));
		}
		
		public function getSolidComponentById(id:String):SolidComponent
		{
			return FlexGlobals.topLevelApplication.project.areas.getSolidComponentById(id);
		}
		
		public function getAllSolidComponents():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.solidComponents;
		}
		
		public function solidComponentWithIdExists(id:String):Boolean
		{
			return FlexGlobals.topLevelApplication.project.areas.solidComponentWithIdExists();
		}
		
		public function solidComponentByOrientationExists(orientation:String):Boolean
		{
			var result:Boolean = false;
			
			switch(orientation)
			{
				case "North":
					
				for each(var solidComponent:SolidComponent in this.getAllSolidComponents())
				{
					if(solidComponent is ComplexSolidComponent)
					{
						if((solidComponent as ComplexSolidComponent).orientation == 1)
						{
							result = true;
						}
					}
				}
					
				break;
				
				case "East":
					
				for each(var solidComponent:SolidComponent in this.getAllSolidComponents())
				{
					if(solidComponent is ComplexSolidComponent)
					{
						if((solidComponent as ComplexSolidComponent).orientation == 90)
						{
							result = true;
						}
					}
				}
					
				break;
				
				case "South":
					
				for each(var solidComponent:SolidComponent in this.getAllSolidComponents())
				{
					if(solidComponent is ComplexSolidComponent)
					{
						if((solidComponent as ComplexSolidComponent).orientation == 180)
						{
							result = true;
						}
					}
				}
					
				break;
				
				case "West":
					
				for each(var solidComponent:SolidComponent in this.getAllSolidComponents())
				{
					if(solidComponent is ComplexSolidComponent)
					{
						if((solidComponent as ComplexSolidComponent).orientation == 270)
						{
							result = true;
						}
					}
				}
					
				break;
				
				case "South-West":
					
				for each(var solidComponent:SolidComponent in this.getAllSolidComponents())
				{
					if(solidComponent is ComplexSolidComponent)
					{
						if((solidComponent as ComplexSolidComponent).orientation == 225)
						{
							result = true;
						}
					}
				}
					
				break;
				
				case "South-East":
					
				for each(var solidComponent:SolidComponent in this.getAllSolidComponents())
				{
					if(solidComponent is ComplexSolidComponent)
					{
						if((solidComponent as ComplexSolidComponent).orientation == 135)
						{
							result = true;
						}
					}
				}
					
				break;
				
				case "North-West":
					
				for each(var solidComponent:SolidComponent in this.getAllSolidComponents())
				{
					if(solidComponent is ComplexSolidComponent)
					{
						if((solidComponent as ComplexSolidComponent).orientation == 315)
						{
							result = true;
						}
					}
				}
					
				break;
				
				case "North-East":
					
				for each(var solidComponent:SolidComponent in this.getAllSolidComponents())
				{
					if(solidComponent is ComplexSolidComponent)
					{
						if((solidComponent as ComplexSolidComponent).orientation == 45)
						{
							result = true;
						}
					}
				}
					
				break; 
			}
				
				return result;
		}
		
		public function getSolidComponentByType(type:String):ArrayCollection
		{
			var result:ArrayCollection = new ArrayCollection();
			
			for each(var solidComponent:SolidComponent in this.getAllSolidComponents())
			{
				if(solidComponent.description == type)
				{
					result.addItem(solidComponent);
				}
			}
			
			return result;
		}
		
		public function getComplexSolidComponentById(id:String):ComplexSolidComponent
		{
			return FlexGlobals.topLevelApplication.project.areas.getComplexSolidComponentById(id);
		}
		
		public function getSolidComponentsWithAttachedConstructionByType(type:String):ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.getSolidComponentsWithAttachedConstructionsByType(type);
		}
		
		public function getAllSolidComponentsWithAttachedConstruction():ArrayCollection
		{
			var resourceManager:IResourceManager = ResourceManager.getInstance();
			var result:ArrayCollection = new ArrayCollection();
			
			result.addAll(this.getSolidComponentsWithAttachedConstructionByType(resourceManager.getString('systemtexts','exteriorWallToAmbientAir')));
			result.addAll(this.getSolidComponentsWithAttachedConstructionByType(resourceManager.getString('systemtexts','exteriorWallToGround')));
			result.addAll(this.getSolidComponentsWithAttachedConstructionByType(resourceManager.getString('systemtexts','externalWallToCellar')));
			result.addAll(this.getSolidComponentsWithAttachedConstructionByType(resourceManager.getString('systemtexts','externalWallPartitionWall')));
			result.addAll(this.getSolidComponentsWithAttachedConstructionByType(resourceManager.getString('systemtexts','roofAreaSloped')));
			result.addAll(this.getSolidComponentsWithAttachedConstructionByType(resourceManager.getString('systemtexts','roofAreaFlat')));
			result.addAll(this.getSolidComponentsWithAttachedConstructionByType(resourceManager.getString('systemtexts','groundSlab')));
			result.addAll(this.getSolidComponentsWithAttachedConstructionByType(resourceManager.getString('systemtexts','groundSlabAgainstCellar')));
			
			return result;
		}
		
		public function calculateMeanUValueOfSolidComponentsByType(type:String):Number
		{
			return FlexGlobals.topLevelApplication.project.areas.calculateMeanUValueOfSolidComponentsByType(type);
		}
		
		public function updateSolidComponentUISection():void
		{
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).updateSolidComponentUISection(FlexGlobals.topLevelApplication.project.areas.solidComponents);
		}
		
			public function updateSolidComponentAreaField(solidComponent:SolidComponent):void
		{
			var rowNumber:Number;
			
			for each(var row:HBox in (FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).solidComponentsForm.getChildren())
			{
				if((row.getChildAt(2) as TextInput).text == solidComponent.id)
				{
					rowNumber = (FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).solidComponentsForm.getChildIndex(row);
				}
				
			}

			if(rowNumber > -1)
			{
				(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).updateSolidComponentAreaField(rowNumber, solidComponent.area);
			}
		}
		
		public function addComplexSolidComponent(complexSolidComponent:ComplexSolidComponent):void
		{
			FlexGlobals.topLevelApplication.project.areas.addSolidComponent(complexSolidComponent);
		}
		
		public function removeComplexSolidComponent(complexSolidComponent:ComplexSolidComponent):void
		{
			FlexGlobals.topLevelApplication.project.areas.removeSolidComponent(complexSolidComponent);
		}
		
		public function detachConstructionBySolidComponentId(id:String):void
		{
			FlexGlobals.topLevelApplication.project.areas.detachConstructionBySolidComponentId(id);
		}
		
		public function getWindowsBySolidComponent(solidComponent:ComplexSolidComponent):ArrayCollection
		{
			return solidComponent.windows;
		}
		
		public function getWindowById(id:String):TransparentComponent
		{
		  return FlexGlobals.topLevelApplication.project.areas.getWindowById(id);
		}
		
		public function detachConstructionByWindowId(id:String):void
		{
			FlexGlobals.topLevelApplication.project.areas.detachConstrcutionByWindowId(id);
		}
		
		public function getAllWindows():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.getAllWindows();
		}
		
		public function getWindowsWithAttachedConstruction():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.getWindowsWithAttachedConstructions();
		}
		
		public function removeWindow(window:TransparentComponent):void
		{
			FlexGlobals.topLevelApplication.project.areas.removeWindow(window);
		}
		
		public function calculateMeanUValueOfWindows():Number
		{
			return FlexGlobals.topLevelApplication.project.areas.calculateMeanUValueOfWindows();
		}
		
		public function calculateMeanGValueOfWindows():Number
		{
			return FlexGlobals.topLevelApplication.project.areas.calculateMeanGValueOfWindows();
		}
		
		public function calculateMeanGValueOfWindowsByOrientation(orientation:String):Number
		{
			return FlexGlobals.topLevelApplication.project.areas.calculateMeanGValueOfWindowsByOrientation(orientation);
		}
		
		public function calculateTotalAreaOfWindowsByOrientation(orientation:String):Number
		{
			return FlexGlobals.topLevelApplication.project.areas.calculateTotalWindowsAreaByOrientation(orientation);
		}		
		
		public function updateWindowAreaField(window:TransparentComponent):void
		{			
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).updateWindowAreaField(window);
		}
		
		public function updateWindowsUISection():void
		{
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).updateWindowsUISection(getAllWindows());
		}
		
		public function getDoorsBySolidComponent(solidComponent:ComplexSolidComponent):ArrayCollection
		{
			return solidComponent.doors;
		}
		
		public function getAllDoors():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.getAllDoors();
		}
		
		public function getDoorById(id:String):TransparentComponent
		{
			return FlexGlobals.topLevelApplication.project.areas.getDoorById(id);
		}
		
		public function detachConstructionByDoorId(id:String):void
		{
			FlexGlobals.topLevelApplication.project.areas.detachConstructionByDoorId(id);
		}
		
		public function getDoorsWithAttachedConstruction():ArrayCollection
		{
			return FlexGlobals.topLevelApplication.project.areas.getDoorsWithAttachedConstructions();
		}
		
		public function removeDoor(door:TransparentComponent):void
		{
			FlexGlobals.topLevelApplication.project.areas.removeDoor(door);
		}
		
		public function updateDoorAreaField(door:TransparentComponent):void
		{			
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).updateDoorAreaField(door);
		}
		
		public function updateDoorsUISection():void
		{
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).updateDoorsUISection(getAllDoors());
		}
		
		public function calculateMeanUvalueDoors():Number
		{
			return FlexGlobals.topLevelApplication.project.areas.calculateMeanUValueOfDoors();
		}
		
		public function addIdToComponentsDropDownListDataProvider(id:String):void
		{
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).addItemToComponentDropDownDataProvider(id);
		}
		
		public function removeIdFromComponentsDropDownListsDataProvider(id:String):void
		{
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).removeItemFromComponentDropDownDataProvider(id);
		}
		
		public function emptyTheCoponentsDropDonwListDataProvider():void
		{
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).emptyComponentDropDownDataProvider();
		}
		
		public function attachedConstructionsExists():Boolean
		{
			return FlexGlobals.topLevelApplication.project.areas.attachedConstructionsExists();
		}

		public function updateAreaInputUIData():void
		{
			this.updateHeatedSpaceAreaUISection();
			this.updateHeatedSpaceVolumeUISection();
			this.updateSolidComponentUISection();
			this.updateWindowsUISection();
			this.updateDoorsUISection();
			(FlexGlobals.topLevelApplication.screenCollection[1] as AreaInput).areaStatus();
		}
		
		public function updateOpaqueOverviewStatus():void
		{
			FlexGlobals.topLevelApplication.overviewPalette.solidComponentStatus();
		}
		
		public function updateAreaOverviewStatus():void
		{
			FlexGlobals.topLevelApplication.overviewPalette.areaStatus();
		}
		
		public function updateAreaOverviewSection():void
		{
			FlexGlobals.topLevelApplication.overviewPalette.updateAreaInputSectionData();
		}
		
		public function getVARatio():Number
		{
			return FlexGlobals.topLevelApplication.project.areas.calculateVARation();
		}
		
		public function getThermalEnvelopeArea():Number
		{
			return FlexGlobals.topLevelApplication.project.areas.getThermlaEnvelopeArea();
		}
	}
}